/**
 * @Company 
 * @Title: LoginController.java 
 * @Package com.jbinfo.qingcaowan.app.controller 
 * @author lizhenwei
 * @date 2016-07-11 下午1:06:21 
 * @version V1.0   
 */
package com.jbinfo.qingcaowan.app.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jbinfo.qingcaowan.base.dto.UserFullInfoDTO;
import com.jbinfo.qingcaowan.base.service.UserFullService;
import com.jbinfo.qingcaowan.common.ClonePojoUtil;
import com.jbinfo.qingcaowan.common.JSONResult;
import com.jbinfo.qingcaowan.common.LoginContextUtil;
import com.jbinfo.qingcaowan.common.ResultCode;
import com.jbinfo.qingcaowan.user.dto.UserDTO;
import com.jbinfo.qingcaowan.user.service.UserService;

/**
 * @ClassName: LoginController
 * @Description: 登录controller
 * @author John Zhao
 */
@Controller
public class LoginController {

	private static final Logger LOG = LoggerFactory.getLogger(LoginController.class);

	@Resource
	private UserService userSerivce;
	
	@Resource
	private UserFullService userFullService;

	/** 
	* @Description: 登录
	* @author John Zhao   
	* @date 2014-11-21 下午1:27:27 
	* @param client
	* @param username
	* @param password
	* @param request
	* @param clientMac   added by lizhenwei 2015-12-01 18:24
	* @return  
	*/ 
	/*@RequestMapping(value = "/{client}/a/login")
	public @ResponseBody
	Object userLogin(@PathVariable("client") String client, String username, String password, HttpServletRequest request, HttpServletResponse response) {
		LOG.info("客户端为 " + client + " 用户名：" + username + " 密码为：" + password);
		
		JSONResult dubboResult = (JSONResult) userSerivce.checkUserAndPwd(username, password);
		
		UserDTO user = new UserDTO();
		
		if (dubboResult != null && ResultCode.SUCCESS.equals(dubboResult.getResultCode())) {
			// 加载用户的登陆信息到session
			user = dubboResult.getResultBean();
			
			// writeUserToSession(user, password,session);
			LoginContextUtil.writeToSession(request, user, username, password);
			
		} else {
			
		}
		// 为解决AngularJs 跨域问题添加
//		response.setHeader("Access-Control-Allow-Origin", "*");// 授权的源控制
//		response.setHeader("Access-Control-Allow-Credentials", "true"); // 控制是否开启与Ajax的Cookie提交方式
//		response.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS,DELETE"); // 允许请求的HTTP Method
//		response.setHeader("Access-Control-Max-Age", "3600"); // 授权的时间
//		response.setHeader("Access-Control-Allow-Headers", "x-requested-with,content-type,Accepte"); // 控制哪些header能发送真正的请求  ,content-type   
		
		response.setHeader("Access-Control-Allow-Origin"
				,"*"); // http://my.dj.com   http://www.dotnetdev.net
		
		response.setHeader("Access-Control-Allow-Credentials", "true");
		response.setHeader("Access-Control-Allow-Methods"
				,"PUT, GET, POST, DELETE, OPTIONS");
		response.setHeader("Access-Control-Allow-Headers"
				,"X-Requested-With,Content-Type,Authorization,Accept,Range,Origin");
		response.setHeader("Access-Control-Expose-Headers"
				,"Content-Range");
		response.setHeader("Access-Control-Max-Age","3600");
		
		return dubboResult;
	}
*/
	@RequestMapping(value = "/{client}/a/login")
	public @ResponseBody
	Object userLogin(@PathVariable("client") String client
			, UserFullInfoDTO userFullInfoDTO
			, HttpServletRequest request, HttpServletResponse response) {
		
		LOG.info("客户端为 " + client + " userFullInfoDTO：" + userFullInfoDTO);
		
		JSONResult dubboResult = (JSONResult) userFullService.getUserFullInfo(userFullInfoDTO);
		
		UserDTO user = new UserDTO();
		
		if (dubboResult != null && ResultCode.SUCCESS.equals(dubboResult.getResultCode())) {
			// 加载用户的登陆信息到session
			UserFullInfoDTO userFullDTO = dubboResult.getResultBean();
			user = ClonePojoUtil.copyClassFromTo(userFullDTO, UserDTO.class);
			
			String username = userFullInfoDTO.getUserName();
			String password = userFullInfoDTO.getPassword();
			// writeUserToSession(user, password,session);
			LoginContextUtil.writeToSession(request, user, username, password);
			
		} else {
			
		}
		// 为解决AngularJs 跨域问题添加
//		response.setHeader("Access-Control-Allow-Origin", "*");// 授权的源控制
//		response.setHeader("Access-Control-Allow-Credentials", "true"); // 控制是否开启与Ajax的Cookie提交方式
//		response.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS,DELETE"); // 允许请求的HTTP Method
//		response.setHeader("Access-Control-Max-Age", "3600"); // 授权的时间
//		response.setHeader("Access-Control-Allow-Headers", "x-requested-with,content-type,Accepte"); // 控制哪些header能发送真正的请求  ,content-type   
		
		response.setHeader("Access-Control-Allow-Origin"
				,"*"); // http://my.dj.com   http://www.dotnetdev.net
		
		response.setHeader("Access-Control-Allow-Credentials", "true");
		response.setHeader("Access-Control-Allow-Methods"
				,"PUT, GET, POST, DELETE, OPTIONS");
		response.setHeader("Access-Control-Allow-Headers"
				,"X-Requested-With,Content-Type,Authorization,Accept,Range,Origin");
		response.setHeader("Access-Control-Expose-Headers"
				,"Content-Range");
		response.setHeader("Access-Control-Max-Age","3600");
		
		return dubboResult;
	}
}
